import { defineStore } from 'pinia'
import { useConversationStore } from './conversation'
import type { Conversation } from '@/types'

export const useChatbotStore = defineStore('chatbot', {
  state: () => ({
    key: window.localStorage.getItem('chatbotKey') || '',
  }),
  getters: {
    getKey: (state) => state.key,
  },
  actions: {
    setKey(key: string) {
      window.localStorage.setItem('chatbotKey', key)
      this.key = key
      const conversationStore = useConversationStore()
      conversationStore.currentKey = key
    },
    setLocalConversationList(conversationList: Conversation[]) {
      window.localStorage.setItem('conversation', JSON.stringify(conversationList))
    },
    getLocalConversationList() {
      const conversationList = window.localStorage.getItem('conversation')
      if (conversationList) {
        return JSON.parse(conversationList) as Conversation[]
      }
      return []
    },
  },
})
